package cn.tedu.web.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.tedu.web.entity.Address;
import cn.tedu.web.entity.Order;
import cn.tedu.web.entity.User;
import cn.tedu.web.entity.Wallet;
import cn.tedu.web.entity.WalletAccount;
import cn.tedu.web.mapper.AddressMapper;
import cn.tedu.web.mapper.UserMapper;
import cn.tedu.web.mapper.WalletMapper;
import cn.tedu.web.service.IAddressService;
import cn.tedu.web.service.IOrderDetailService;
import cn.tedu.web.service.IOrderService;
import cn.tedu.web.service.IUserService;
import cn.tedu.web.service.IWalletAccountService;
import cn.tedu.web.service.IWalletService;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 * user
 * 
 * @author issen
 * @since 2021-09-17
 */
@SuppressWarnings("all")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    public IUserService userService;
    @Autowired
    public UserMapper userMapper;
    @Autowired
    public IWalletService walletService;
    @Autowired
    public IAddressService addressService;
    @Autowired
    public IWalletAccountService walletAccountService;
    @Autowired
    public IOrderService orderService;
    @Autowired
    public IOrderDetailService orderDetailService;

    /**
     * mp分页查询
     * 
     * @param current   : 当前页面
     * @param size      : 页面大小
     * @param "records" : 查询到的数据列表
     */
    @ResponseBody
    @ApiOperation(value = "获取用户列表")
    @RequestMapping("info")
    public ResponseEntity<Object> list(@RequestBody JSONObject param) {
        Long current = (long) 1;
        Long size = (long) 10;
        current = Long.parseLong(param.getString("current"));
        size = Long.parseLong(param.getString("size"));
        // System.out.println(param.getString("current"));
        return new ResponseEntity<>(userMapper.selectPage(new Page<>(current, size), null), HttpStatus.OK);
    }

    @PostMapping("enable")
    public Boolean enable(@RequestBody JSONObject param) {
        Long id = Long.parseLong(param.getString("id"));
        UpdateWrapper updateWrapper = new UpdateWrapper<User>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", 1);
        return userService.update(updateWrapper);
    }

    // 获取用户详细信息
    @GetMapping("/get/{id}")
    public ResponseEntity<Object> getInfo(@PathVariable("id") Integer id) {
        // 基础信息(排除密码)
        User user = userService
                .getOne(new QueryWrapper<User>().eq("id", id).select(User.class,i -> !i.getColumn().equals("password")));
        // 账户余额
        user.setAmount(walletService.getOne(new QueryWrapper<Wallet>().select("amount")).getAmount());
        // 地址
        user.setAddress(addressService
                .getOne(new QueryWrapper<Address>().eq("uid", id).eq("is_default", 1).last("limit 1")).combine());
        // 消费流水
        user.setWalletAccounts(
                walletAccountService.list(new QueryWrapper<WalletAccount>().eq("uid", id).orderByDesc("time").last("limit 10")));
        // 用户订单
        user.setOrders(orderService.list(new QueryWrapper<Order>().eq("uid", id)));
        
        return new ResponseEntity<>(user, HttpStatus.OK);
    }
 
    // 更改用户信息
    @PostMapping("/edit")
    public ResponseEntity<Object> edit(@RequestBody User user) {
        UpdateWrapper updateWrapper = new UpdateWrapper<User>();
        updateWrapper.eq("id", user.getId());
        userService.update(user, updateWrapper);
        return new ResponseEntity<>(user, HttpStatus.OK);
    }

}
